package com.hpeu_01.day0121;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 字节输入流的使用
 */
public class InputStream01 {
    //一次读取一个字节
    @Test
    public void testRead(){
        try(
                InputStream is = new FileInputStream("D:\\project\\JAVA\\java\\JavaSE\\src\\com\\hpeu_01\\day0121\\file\\a.txt");
        ){
            int len = is.read();
            while (len !=-1){
                System.out.print((char) len);
                len = is.read();
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    // 一次读取多个字节
    @Test
    public void testRead02(){
        try(
                InputStream is = new FileInputStream("D:\\project\\JAVA\\java\\JavaSE\\src\\com\\hpeu_01\\day0121\\file\\a.txt");
        ){
            byte[] b = new byte[1];
            int len = is.read(b);
            while (len !=-1){
                System.out.print(new String(b,0,len));
                len = is.read(b);
            }
        }catch(IOException e){

        }
    }

    //记住这种写法
    @Test
    public void testRead03(){
        try(
                InputStream is = new FileInputStream("D:\\project\\JAVA\\java\\JavaSE\\src\\com\\hpeu_01\\day0121\\file\\a.txt");
        ){
            //1.创建字节数组
            byte[] b = new byte[2];
            //2.定义读取的长度
            int len = -1;
            while ((len=is.read(b)) != -1){
                System.out.println(new String(b, 0, len));
            }

        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
